/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-10-17         wei li
 */

package com.skyworth.badsdk;

import java.io.File;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.skyworth.framework.skysdk.logger.SkyLogger;
import com.skyworth.framework.skysdk.properties.SkyXmlHelper;


/**
 * <p>Description:</p>
 * <p>获取属性的基类</p>
 *
 * @author wei li
 * @version V1.0.0
 * @ClassName BaseProperties
 * @date 2013-10-29
 */
public class BaseProperties {
    public static String SKY_DIR = "/skydir";

    //    public static String SKY_DIR = "E:";
    public static String loadConfig(String configName, String mNamekey, String mValueKey, List<String> mProperties) {
        String configDocPath = SKY_DIR + File.separator + "config" + File.separator + configName;
        String generalConfigsStr = "@str:" + "SKY_DIR=" + SKY_DIR + ";" + "RES_DIR=" + SKY_DIR
                + "/res;" + "PLUGIN_DIR=" + SKY_DIR + "/plugins;" + "TEMP_DIR=" + SKY_DIR
                + "/temp;" + "DB_FILE=" + SKY_DIR + "/database/db.data;" + "MENU_DIR=" + SKY_DIR
                + "/menu;" + "DB_DIR=" + SKY_DIR + "/database/;" + "VOICE_RES_DIR=" + SKY_DIR + "/voice;";
        System.out.println("cofnigDocPath=" + configDocPath);
        File generalConfig = new File(configDocPath);
        if (!generalConfig.exists()) {
            SkyLogger.e("lw", configDocPath + " not exsit!");
            return generalConfigsStr;
        }
        Document doc = SkyXmlHelper.createDocFromFile(configDocPath);
        Node root = doc.getFirstChild();
        while (true) {
            if (root == null) {
                break;
            }

            if (root.getNodeName().equals("#text") || root.getNodeName().equals("#comment")) {
                root = root.getNextSibling();
            } else {
                break;
            }
        }

        if (root == null) {
            SkyLogger.e("lw", "Read " + configDocPath + " file error!");
            return null;
        }

        NodeList items = root.getChildNodes();
        for (int i = 0; i < items.getLength(); i++) {
            Node item = items.item(i);
            if (item.getNodeName() == null) {
                continue;
            }
            if (!(item.getNodeName().equals("#text") || item.getNodeName().equals("#comment"))) {
                if (item.getAttributes().getNamedItem(mNamekey) == null)
                    continue;
                if (mProperties != null) {
                    mProperties.add(item.getAttributes().getNamedItem(mNamekey).getNodeValue());
                }
                generalConfigsStr += item.getAttributes().getNamedItem(mNamekey).getNodeValue() + "="
                        + item.getAttributes().getNamedItem(mValueKey).getNodeValue() + ";";
            }
        }
        String productGeneralConfigStr = loadProductConfig(configName, mNamekey, mValueKey, mProperties);
        if (productGeneralConfigStr != null) {
            generalConfigsStr += productGeneralConfigStr;
        }
        return generalConfigsStr;
    }

    public static String loadProductConfig(String configName, String mNamekey, String mValueKey, List<String> mProperties) {
        String configDocPath = SKY_DIR + File.separator + "config" + File.separator + "product" + File.separator + configName;
        String generalConfigsStr = null;
        try {
            System.out.println("product cofnigDocPath=" + configDocPath);
            File generalConfig = new File(configDocPath);
            if (!generalConfig.exists()) {
                SkyLogger.e("lw", configDocPath + " not exsit!");
                return generalConfigsStr;
            }
            Document doc = SkyXmlHelper.createDocFromFile(configDocPath);
            Node root = doc.getFirstChild();
            while (true) {
                if (root == null) {
                    break;
                }

                if (root.getNodeName().equals("#text") || root.getNodeName().equals("#comment")) {
                    root = root.getNextSibling();
                } else {
                    break;
                }
            }

            if (root == null) {
                SkyLogger.e("lw", "Read " + configDocPath + " file error!");
                return null;
            }

            NodeList items = root.getChildNodes();
            for (int i = 0; i < items.getLength(); i++) {
                Node item = items.item(i);
                if (item.getNodeName() == null) {
                    continue;
                }
                if (!(item.getNodeName().equals("#text") || item.getNodeName().equals("#comment"))) {
                    if (item.getAttributes().getNamedItem(mNamekey) == null)
                        continue;
                    if (mProperties != null) {
                        mProperties.add(item.getAttributes().getNamedItem(mNamekey).getNodeValue());
                    }
                    String temp = item.getAttributes().getNamedItem(mNamekey).getNodeValue() + "="
                            + item.getAttributes().getNamedItem(mValueKey).getNodeValue() + ";";
                    if (generalConfigsStr == null) {
                        generalConfigsStr = temp;
                    } else {
                        generalConfigsStr += temp;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return generalConfigsStr;
    }
}
